home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
- ATsis -- create SIS out of depth information
- (C) 2/1995 by Christian Perle
- based on sirds program by W.A. Steer
- *************************************************************************/
-
- #include <stdio.h>
-
- #ifndef SEEK_SET
- #define SEEK_SET 0 /* from beginning of file */
- #define SEEK_CUR 1 /* from current location */
- #define SEEK_END 2 /* from end of file */
- #endif
-
- #define SI_NAME "sis.ppm"
-
- /* depth of background in pixels */
- #define BKDEPTH -800
- /* eye separation in pixels */
- #define E 180
- /* observer-screen distance in pixels */
- #define O 700
- /* oversampling ratio (1, 2, 4 or 6) */
- #define OVERSAM 6
- /* slow hidden point removal on/off */
- #define DOHIDDENREM 0
- /* width of picture in pixels */
- #define XRES 640
- /* height of picture in pixels */
- #define YRES 470
- /* width of tile in pixels */
- #define TXR 96
- /* height of tile in pixels */
- #define TYR 470
-
- unsigned char thedepth[XRES*YRES];
- unsigned char pixels[XRES*OVERSAM*3];
- unsigned char thesird[XRES*(YRES+10)*3]; /* add 10 "help lines" */
- unsigned char tile[TXR*TYR*3];
- int z[XRES];
- int link[XRES*OVERSAM];
-
- FILE *fd;
-
- /* read depth file */
- read_depth(name)
- char *name;
- {
- int x;
-
- fd = fopen(name, "rb");
- if (fd == NULL) {
- fprintf(stderr, "can't open %s\n", name);
- exit(0);
- }
- for (x = 0; x < XRES*YRES; x++) thedepth[x] = fgetc(fd);
- fclose(fd);
- }
-
- /* read tile file */
- read_tile(name)
- char *name;
- {
- int x;
-
- fd = fopen(name, "rb");
- if (fd == NULL) {
- fprintf(stderr, "can't open %s\n", name);
- exit(0);
- }
- fseek(fd, 18L, SEEK_SET); /* skip 18 header bytes */
- for (x = 0; x < TXR*TYR*3; x++) tile[x] = fgetc(fd);
- fclose(fd);
- }
-
- /* add help marks */
- init_sis()
- {
- int x, y, th;
- float helpdist;
-
- /* clear thesird[] array */
- for (x = 0; x < XRES*(YRES+10)*3; x++)
- thesird[x] = 0;
-
- helpdist = abs(BKDEPTH)*E/(2.0*(abs(BKDEPTH)+O));
- th = 0;
- for (y = 9; y >= 0; y--) {
- /* left mark */
- for (x = XRES/2-helpdist-th; x <= XRES/2-helpdist+th; x++) {
- thesird[3*(y*XRES+x)] = 255; /* blue */
- thesird[3*(y*XRES+x)+1] = 255; /* green */
- thesird[3*(y*XRES+x)+2] = 255; /* red */
- }
- /* right mark */
- for (x = XRES/2+helpdist-th; x <= XRES/2+helpdist+th; x++) {
- thesird[3*(y*XRES+x)] = 255; /* blue */
- thesird[3*(y*XRES+x)+1] = 255; /* green */
- thesird[3*(y*XRES+x)+2] = 255; /* red */
- }
- th++;
- }
- }
-
- /* render the SIS */
- do_sis()
- {
- int x, y, h, u, dx, xx, highest, separation, left, right, pp, tilep;
- int r, g, b, visible;
- float v;
-
- for (y = 0; y < YRES; y++) {
- fprintf(stderr, "Rendering line %4d of %4d\r", y+1, YRES);
- for (x = 0; x < XRES*OVERSAM; x++) link[x] = x;
- highest = BKDEPTH;
- for (x = 0; x < XRES; x++) {
- h = BKDEPTH + thedepth[y*XRES+x];
- z[x] = h;
- if (h > highest) highest = h;
- }
- for (x = 0; x < XRES*OVERSAM; x++) {
- separation = (E*OVERSAM*z[x/OVERSAM])/(z[x/OVERSAM]-O);
- left = x-separation/2;
- right = left+separation;
- if ((left >= 0) && (right < XRES*OVERSAM)) {
- visible = 1;
- if (DOHIDDENREM == 1) {
- v = 2.0*(O-z[x/OVERSAM])/E;
- dx = 1;
- do {
- u = z[x/OVERSAM]+dx*v;
- if ((z[(x+dx)/OVERSAM] >= u) || (z[(x-dx)/OVERSAM] >= u))
- visible = 0;
- dx++;
- } while ((u <= highest) && (visible == 1));
- }
- if (visible == 1) link[right] = left;
- }
- }
- for (x = 0; x < XRES*OVERSAM; x++) {
- tilep = (y % TYR)*TXR + (x / OVERSAM) % TXR;
- if (link[x] == x) {
- pixels[3*x] = tile[3*tilep];
- pixels[3*x+1] = tile[3*tilep+1];
- pixels[3*x+2] = tile[3*tilep+2];
- }
- else {
- pixels[3*x] = pixels[3*link[x]];
- pixels[3*x+1] = pixels[3*link[x]+1];
- pixels[3*x+2] = pixels[3*link[x]+2];
- }
- }
- for (x = 0; x < XRES; x++) {
- xx = x*OVERSAM;
- switch (OVERSAM) {
- case 1:
- b = pixels[3*xx];
- g = pixels[3*xx+1];
- r = pixels[3*xx+2];
- break;
- case 2:
- if ((x > 0) && (x < XRES-1)) {
- b = pixels[3*xx]*42+(pixels[3*(xx-1)]+pixels[3*(xx+1)])*24
- +(pixels[3*(xx-2)]+pixels[3*(xx+2)])*5;
- b = b/100;
- g = pixels[3*xx+1]*42+(pixels[3*(xx-1)+1]+pixels[3*(xx+1)+1])*24
- +(pixels[3*(xx-2)+1]+pixels[3*(xx+2)+1])*5;
- g = g/100;
- r = pixels[3*xx+2]*42+(pixels[3*(xx-1)+2]+pixels[3*(xx+1)+2])*24
- +(pixels[3*(xx-2)+2]+pixels[3*(xx+2)+2])*5;
- r = r/100;
- }
- else {
- b = pixels[3*xx];
- g = pixels[3*xx+1];
- r = pixels[3*xx+2];
- }
- break;
- case 4:
- if ((x > 0) && (x < XRES-1)) {
- b = pixels[3*xx]*26+(pixels[3*(xx-1)]+pixels[3*(xx+1)])*18
- +(pixels[3*(xx-2)]+pixels[3*(xx+2)])*12
- +(pixels[3*(xx-3)]+pixels[3*(xx+3)])*7;
- b = b/100;
- g = pixels[3*xx+1]*26+(pixels[3*(xx-1)+1]+pixels[3*(xx+1)+1])*18
- +(pixels[3*(xx-2)+1]+pixels[3*(xx+2)+1])*12
- +(pixels[3*(xx-3)+1]+pixels[3*(xx+3)+1])*7;
- g = g/100;
- r = pixels[3*xx+2]*26+(pixels[3*(xx-1)+2]+pixels[3*(xx+1)+2])*18
- +(pixels[3*(xx-2)+2]+pixels[3*(xx+2)+2])*12
- +(pixels[3*(xx-3)+2]+pixels[3*(xx+3)+2])*7;
- r = r/100;
- }
- else {
- b = pixels[3*xx];
- g = pixels[3*xx+1];
- r = pixels[3*xx+2];
- }
- break;
- case 6:
- if ((x > 0) && (x < XRES-1)) {
- b = pixels[3*xx]*14+(pixels[3*(xx-1)]+pixels[3*(xx+1)])*14
- +(pixels[3*(xx-2)]+pixels[3*(xx+2)])*11
- +(pixels[3*(xx-3)]+pixels[3*(xx+3)])*8
- +(pixels[3*(xx-4)]+pixels[3*(xx+4)])*5
- +(pixels[3*(xx-5)]+pixels[3*(xx+5)])*3
- +(pixels[3*(xx-6)]+pixels[3*(xx+6)])*2;
- b = b/100;
- g = pixels[3*xx+1]*14+(pixels[3*(xx-1)+1]+pixels[3*(xx+1)+1])*14
- +(pixels[3*(xx-2)+1]+pixels[3*(xx+2)+1])*11
- +(pixels[3*(xx-3)+1]+pixels[3*(xx+3)+1])*8
- +(pixels[3*(xx-4)+1]+pixels[3*(xx+4)+1])*5
- +(pixels[3*(xx-5)+1]+pixels[3*(xx+5)+1])*3
- +(pixels[3*(xx-6)+1]+pixels[3*(xx+6)+1])*2;
- g = g/100;
- r = pixels[3*xx+2]*14+(pixels[3*(xx-1)+2]+pixels[3*(xx+1)+2])*14
- +(pixels[3*(xx-2)+2]+pixels[3*(xx+2)+2])*11
- +(pixels[3*(xx-3)+2]+pixels[3*(xx+3)+2])*8
- +(pixels[3*(xx-4)+2]+pixels[3*(xx+4)+2])*5
- +(pixels[3*(xx-5)+2]+pixels[3*(xx+5)+2])*3
- +(pixels[3*(xx-6)+2]+pixels[3*(xx+6)+2])*2;
- r = r/100;
- }
- else {
- b = pixels[3*xx];
- g = pixels[3*xx+1];
- r = pixels[3*xx+2];
- }
- break;
- }
- thesird[3*((y+10)*XRES+x)] = b; /* blue */
- thesird[3*((y+10)*XRES+x)+1] = g; /* green */
- thesird[3*((y+10)*XRES+x)+2] = r; /* red */
- }
- }
- }
-
- /* write SIS as TGA file */
- write_sis_tga()
- {
- int x;
-
- fd = fopen(SI_NAME, "wb");
- fputc(0,fd);fputc(0,fd);fputc(2,fd);
- fputc(0,fd);fputc(0,fd);fputc(0,fd);fputc(0,fd);fputc(0,fd);
- fputc(0,fd);fputc(0,fd);fputc(0,fd);fputc(0,fd);
- fputc(XRES % 256,fd);fputc(XRES / 256,fd);
- fputc((YRES+10) % 256,fd);fputc((YRES+10) / 256,fd);
- fputc(24,fd);fputc(32,fd);
- for (x = 0; x < XRES*(YRES+10)*3; x++) fputc(thesird[x],fd);
- fclose(fd);
- }
-
- /* write SIS as PPM file */
- write_sis_ppm()
- {
- int x;
-
- fd = fopen(SI_NAME, "wb");
- fprintf(fd, "P6\n");
- fprintf(fd, "%d %d\n", XRES, YRES+10);
- fprintf(fd, "255\n");
- for (x = 0; x < XRES*(YRES+10); x++) {
- fputc(thesird[3*x+2],fd);
- fputc(thesird[3*x+1],fd);
- fputc(thesird[3*x],fd);
- }
- fclose(fd);
- }
-
- /* --------------------------------------------------------------------- */
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- int argn;
- char *de_name, *ti_name;
-
- fprintf(stderr, "ATsis v1.0 (C) 2/1995 Christian Perle\n");
- fprintf(stderr, "based on sirds program by W.A. Steer\n\n");
- argn = 1; /* number of arguments */
- if (argn < argc) {
- de_name = argv[argn];
- argn++;
- if (argn < argc) {
- ti_name = argv[argn];
- argn++;
- }
- else { /* too few arguments */
- fprintf(stderr, "usage: atsis depthfile tilefile\n");
- exit(0);
- }
- }
- else { /* too few arguments */
- fprintf(stderr, "usage: atsis depthfile tilefile\n");
- exit(0);
- }
- if (argn != argc) { /* too much arguments */
- fprintf(stderr, "usage: atsis depthfile tilefile\n");
- exit(0);
- }
- fprintf(stderr, "Rendering %s to ", de_name);
- fprintf(stderr, SI_NAME);
- fprintf(stderr, "\n");
- fprintf(stderr, "Using tile file %s\n", ti_name);
- fprintf(stderr, "Size %dx%d, ", XRES, YRES+10);
- if (OVERSAM == 1) fprintf(stderr, "no oversampling");
- else fprintf(stderr, "%d times oversampling", OVERSAM);
- if (DOHIDDENREM == 1) fprintf(stderr, ", hidden point removal.\n\n");
- else fprintf(stderr, ".\n\n");
- read_depth(de_name);
- read_tile(ti_name);
- init_sis();
- do_sis();
- fprintf(stderr, "\n");
- write_sis_ppm();
- fprintf(stderr, "Done.\n");
- }
-